package com.zscat.mallplus.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.zscat.mallplus.annotation.IgnoreAuth;
import com.zscat.mallplus.dms.service.IQsgStoreService;
import com.zscat.mallplus.enums.AssistantStatus;
import com.zscat.mallplus.enums.OrderStatus;
import com.zscat.mallplus.qsg.*;
import com.zscat.mallplus.ums.entity.UmsMember;
import com.zscat.mallplus.utils.CommonResult;
import com.zscat.mallplus.utils.DateUtil;
import com.zscat.mallplus.utils.UserUtils;
import com.zscat.mallplus.vo.MealDate;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.*;

/**
 * 助教管理Controller
 */
@RestController
@Api(tags = "QsgTeachingAssistantController", description = "助教管理")
@RequestMapping("/api/qsg/teachingassistant")
@Slf4j
public class QsgTeachingAssistantController {

    @Autowired
    private IQsgTeachingAssistantService teachingAssistantService;

    @Autowired
    private IQsgTeachingAssistantorderService teachingAssistantorderService;

    @IgnoreAuth
    @ApiOperation("查询所有的助教列表")
    @GetMapping(value = "/list")
    public Object getAll() {
        try {
            QueryWrapper qw = new QueryWrapper();
            qw.orderByDesc("sort");
            List<QsgTeachingAssistant> assistants = teachingAssistantService.list(qw);
            QueryWrapper orderqw = new QueryWrapper();
            orderqw.eq("order_status", OrderStatus.HAS_PAYMONEY.getValue());
            orderqw.le("start_time", DateUtil.dateToString(new Date(), "yyyy/MM/dd HH:mm:ss"));
            orderqw.ge("end_time", DateUtil.dateToString(new Date(), "yyyy/MM/dd HH:mm:ss"));
            for (QsgTeachingAssistant assistant:assistants
                 ) {
                Long assistantId = assistant.getId();
                orderqw.eq("assistant_id", assistantId);
                List<QsgTeachingAssistantorder> assistantorders = teachingAssistantorderService.list(orderqw);
                if(null != assistantorders && !assistantorders.isEmpty()){
                    assistant.setCurrentStatus(AssistantStatus.CAN_NOT_YUYUE.getValue());
                }else{
                    assistant.setCurrentStatus(AssistantStatus.CAN_YUYUE.getValue());
                }
            }
            return new CommonResult().success(assistants);
        } catch (Exception e) {
            log.error("获取助教异常:{}",e.getMessage());
            return new CommonResult().failed();
        }
    }

    @IgnoreAuth
    @ApiOperation("根据助教ID查询助教详情")
    @GetMapping(value = "/getById")
    public Object getById(@RequestParam Long id) {
        try {
            QsgTeachingAssistant assistant = teachingAssistantService.getById(id);
            return new CommonResult().success(assistant);
        } catch (Exception e) {
            log.error("获取助教异常:{}",e.getMessage());
            return new CommonResult().failed();
        }
    }

    @IgnoreAuth
    @GetMapping(value = "/getDates")
    public Object getDates() {
        List<MealDate> dates = DateUtil.getDateForMeal();
        return new CommonResult().success(dates);
    }
}
